//
//  InputCommentViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/4.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "InputCommentViewModel.h"
#import "InputCommentViewModel.h"
#import "InputCommentView.h"
#import "CYEmoticonKeyboardView.h"

@interface InputCommentViewModel()<UITextViewDelegate>
{
    UIView *selfView;
}
//表情键盘view
@property (nonatomic, strong) CYEmoticonKeyboardView *emoticonView;
@property (nonatomic, strong) InputCommentView *commentView;

@end

@implementation InputCommentViewModel

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super initWithSuperView:superView]) {
        
        selfView = superView;
        _commentView = [[InputCommentView alloc] initWithInSuperView:superView];
        kWeakSelf(self);
        _commentView.BlockExpression = ^(GLGWButton * _Nullable expressionBtn) {
//            [weakself expresssionAction:expressionBtn];
        };
        _commentView.botTextView.delegate = self;
        // 监听键盘变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    return self;
}

- (void)expresssionAction:(GLGWButton *)btn
{
    //表情按钮
    [self.commentView.botTextView resignFirstResponder];
//    self.commentView.botTextView.inputView = (btn.selected) ? nil : self.emoticonView ;
//    self.commentView.botTextView.inputView = self.emoticonView;
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    lab.backgroundColor = kRandomColor;
    lab.text = @"sdfadf";
    [vi addSubview:lab];
    vi.backgroundColor = kRandomColor;
    self.commentView.botTextView.inputView = vi;
    [self.commentView.botTextView becomeFirstResponder];
    btn.selected = !btn.selected;
}

- (void)keyboardChanged:(NSNotification *)notification
{
    // UIKeyboardFrameEndUserInfoKey ＝》将要变化的大小
    CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取得动画的持续时间
    CGFloat duration = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    // 设置约束
    [UIView animateWithDuration:duration animations:^{
        self.commentView.y = KScreenHeight - keyboardRect.origin.y;
        [self->selfView layoutIfNeeded];
    }];
//    if (keyboardRect.origin.y == KScreenHeight) {
//        //键盘退出
//        [self.commentView.botTextView endEditing:YES];
//        self.commentView.botTextView.inputView = nil;
//    } else {
////         self->commentView.botTextView.hidden = NO;
//    }
    if (keyboardRect.origin.y == KScreenHeight) {
        self.commentView.botTextView.hidden = YES;
    } else {
        self.commentView.botTextView.hidden = NO;
    }
}

#pragma mark -UITextViewDelegete
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    //    // 拦截用户的回车
    if ([text isEqualToString:@"\n"]) {
        //发送操作回复或者回答
        if (self.BlockReturnTextView) {
            self.BlockReturnTextView((GLGWTextView *)textView, textView.text);
        }
        return NO;
    }
    return YES;
}

//文字变化的时候，显示隐藏提示文字中
- (void)textViewDidChange:(UITextView *)textView
{
    //限制字数360位
    if (textView.text.length > 360) {
        textView.text = [textView.text substringToIndex:360];
    }
    
    if (textView.text.length > 0) {
    } else {
    }
}

#pragma mark - 懒加载
- (CYEmoticonKeyboardView *)emoticonView
{
    if (!_emoticonView) {
        _emoticonView = [[CYEmoticonKeyboardView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, (KScreenWidth/7+5) * 2)];
        _emoticonView.backgroundColor = kRandomColor;
        //赋值block
        __weak typeof(self) weakSelf = self;
        _emoticonView.InsertEmoticon = ^(CYEmoticonCell *cell){
            //插入图片
            [weakSelf.commentView.botTextView insertEmoticon:cell];
            //手动调用代理方法
            [weakSelf textViewDidChange:weakSelf.commentView.botTextView];
        };
    }
    return _emoticonView;
}

@end
